﻿using System;
using System.Diagnostics;

namespace PlayTrace
{
    static class TestTraceFilter
    {
        private static void TestFilterByEventType()
        {
            var listener = new ConsoleTraceListener();
            listener.Filter = new EventTypeFilter(SourceLevels.Warning);

            Trace.Listeners.Clear();
            Trace.Listeners.Add(listener);

            Trace.TraceInformation("information");
            Trace.TraceWarning("warning");
            Trace.TraceError("error");
        }

        private static TraceSource MakeTraceSource(string name, TraceListener listener)
        {
            TraceSource ts = new TraceSource(name);
            Debug.Assert(ts.Switch.Level == SourceLevels.Off);
            ts.Switch.Level = SourceLevels.Information;

            ts.Listeners.Clear();
            ts.Listeners.Add(listener);

            return ts;
        }

        private static void TestFilterBySource()
        {
            var listener = new ConsoleTraceListener();
            listener.Filter = new SourceFilter("source2");

            TraceSource[] traceSources = new TraceSource[]
                                             {
                                                 MakeTraceSource("source1",listener),
                                                 MakeTraceSource("source2",listener)
                                             };

            foreach (TraceSource ts in traceSources)
            {
                ts.TraceInformation("information");
                ts.TraceEvent(TraceEventType.Warning, 0, "warning");
            }
        }

        public static void TestMain()
        {
            // TestFilterByEventType();
            TestFilterBySource();
        }
    }
}
